#pragma once
//============================================================================
// Copyright (C) 2008-2013 Brett R. Jones 
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses 
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include <VxUtilLib/VxMutex.h>

class VxSktBuf
{
public:
	//=== constructor ===//
	VxSktBuf( int iBufSize = 16384 );
	//=== destructor ===//
    virtual ~VxSktBuf();

	//=== read from buf functions called by application ===//
	//! get amount of date available
	int		GetSktBufDataLen( void );
	// app should get the buffer ( this also locks it from being modified by threads )
	// then read the data then call Amount read
	char *	GetSktReadBuf( void );
	//! tell buffer how much was read.. also releases lock.. return -1 if larger amount than possible
	RCODE	SktBufAmountRead( int iAmountRead );
	//! Get the size of the socket buffer
	int		GetSktBufSize( void )		{ return m_iSktBufSize; }

	
	//=== write to buf functions called by socket threads ===//
	//! get amount of space available
	int		GetSktBufFreeSpace( void );
	//! get the buffer to write into.. this also locks it
	U8 *	GetSktWriteBuf( void );
	//! tell buffer how much was written.. also releases lock.. return -1 if larger amount than possible
	RCODE	SktBufAmountWrote( int iAmountWrote );

	//=== first packet ===//
	//! returns true if app has not set first rx packet to false
	BOOL	IsFirstRxPacket( void )						{ return m_bFirstRxPacket; }
	//! set if first tx packet or not
	void	SetIsFirstRxPacket( BOOL bIsFirstPacket )	{ m_bFirstRxPacket = bIsFirstPacket; }
	//! returns true if app has not set first rx packet to false
	BOOL	IsFirstTxPacket( void )						{ return m_bFirstTxPacket; }
	//! set if first tx packet or not
	void	SetIsFirstTxPacket( BOOL bIsFirstPacket )	{ m_bFirstTxPacket = bIsFirstPacket; }

	//! lock
	void	DoLock( void )		{ m_oMutex.Lock();}
	//! lock
	void	DoUnlock( void )	{ m_oMutex.Unlock();}

	//! reset skt buffer
	void	ResetSktBuf( void )
	{
		m_iSktBufIdx = 0;
		m_bFirstRxPacket = true;
		m_bFirstTxPacket = true;
		m_u32RxDecryptedLen = 0;
	}



	//=== vars ===//
	U8 *			m_pau8SktBuf;			// socket buffer
	int				m_iSktBufSize;			// total size of buffer

	int				m_iSktBufIdx;			// current index into buffer
	BOOL			m_bFirstRxPacket;		// app can set if is first packet or not
	BOOL			m_bFirstTxPacket;		// app can set if is first packet or not
	U32				m_u32RxDecryptedLen;	// amount of receive data decrypted

	VxMutex			m_oMutex;
};
